package com.gomore.opple.module.lottery;

import android.os.Bundle;
import android.view.KeyEvent;

import com.gomore.opple.R;
import com.gomore.opple.model.EventActionFinish;
import com.gomore.opple.module.BaseActivity;
import com.gomore.opple.module.IntentStart;

import javax.inject.Inject;

import de.greenrobot.event.EventBus;

/**
 * Created by asus on 2017/5/9.
 */
public class LotteryActivity extends BaseActivity {
    @Inject
    LotteryPresenter lotteryPresenter;

    private LotteryFragment lotteryFragment;
    private String lotteryId;
    private String orderNumber;
    private String drawId;
    private String fragmentType;

    @Override
    protected void initalizeViewData() {
        super.initalizeViewData();
        if (getIntent() != null && getIntent().getStringExtra(IntentStart.LOTTERYID) != null) {
            lotteryId = getIntent().getStringExtra(IntentStart.LOTTERYID);
            orderNumber = getIntent().getStringExtra(IntentStart.ORDERID);
            drawId = getIntent().getStringExtra(IntentStart.DRAWID);
            fragmentType = getIntent().getStringExtra(IntentStart.FRAGMENT_TYPE);
        }
    }

    @Override
    protected void initalizeViews() {
        if (lotteryFragment == null) {
            lotteryFragment = LotteryFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putString(IntentStart.LOTTERYID, lotteryId);
            bundle.putString(IntentStart.ORDERID, orderNumber);
            bundle.putString(IntentStart.DRAWID, drawId);
            bundle.putString(IntentStart.FRAGMENT_TYPE, fragmentType);
            lotteryFragment.setArguments(bundle);
            replaceFragment(lotteryFragment, false, "lottery_view");
        }
    }

    @Override
    protected void initializeDependencyInjector() {
        DaggerLotteryComponent.builder()
                .dataRepositoryComponent(getRepositoryComponent())
                .lotteryPresenterModule((new LotteryPresenterModule(lotteryFragment)))
                .build()
                .inject(this);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.activity_lottery;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            EventActionFinish eventActionFinish = new EventActionFinish();
            eventActionFinish.setType(fragmentType);
            eventActionFinish.setIsFinish(true);
            EventBus.getDefault().post(eventActionFinish);
        }
        return super.onKeyDown(keyCode, event);
    }
}
